package com.vict.util;

import com.vict.config.Common;

import java.io.UnsupportedEncodingException;

public class BitcodeJiamiUtil {
    public static byte[] bitcode(byte[] str) {
        int len = str.length;
        byte[] wen = new byte[len];
        for (int i = 0; i < len; i++) {
            wen[i] = (byte) (str[i] ^ 2);
        }
        return wen;
    }
    public static String encript(String str) {
        if(str == null){
            return str;
        }
        byte[] bytes1 = new byte[0];
        try {
            bytes1 = str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        byte[] bytes2 = bitcode(bytes1);
        String encode = Base58.encode(bytes2);
        encode = Common.version1Encoding + encode;
        return encode;
    }

    public static String decript(String str) {
        if(str == null){
            return str;
        }
        if(str != null && str.startsWith(Common.version1Encoding)){
            str = str.substring(Common.version1Encoding.length(), str.length());
            byte[] decode = Base58.decode(str);
            byte[] bytes2 = bitcode(decode);
            String s = null;
            try {
                s = new String(bytes2, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            return s;
        }else{
            return str;
        }
    }
}
